<template>
  <el-dialog v-model="show" width="1000px" :title="title" append-to-body :close-on-click-modal="false">
    <el-form
      ref="restaurantRef" :model="form" :rules="rules" label-width="80px"
      :disabled="isView"
    >
      <el-row :gutter="20">
        <el-col>
          <el-form-item label="饭店图片" prop="photos">
            <image-upload2 v-model="form.photos" :disabled="isView" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="饭店名称" prop="name" style="width: 100%">
            <el-input v-model="form.name" placeholder="请输入酒店名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="饭店地址" prop="address" style="width: 100%">
            <el-cascader
              v-model="regionIds"
              style="width:100%"
              :options="regionsList"
              :props="{ label:'text'}"
              placeholder="请选择省市区"
            />
            <el-input v-model="form.address" type="textarea" placeholder="请输入详细地址" rows="3" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="饭店简介" prop="introduction" style="width: 100%;">
            <u-editor v-model="form.introduction" placeholder="请输入酒店简介" :disabled="isView" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button v-if="!isView" :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="show=false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>

import { getCurrentInstance, ref, toRefs, watch } from 'vue'
import useVenStore from '@/store/modules/ven'
import { reactive } from '@vue/runtime-core'
import { computed } from '@vue/reactivity'
import useUserStore from '@/store/modules/user'
import { addRestaurant, updateRestaurant } from '@/api/logex/food'

const { proxy } = getCurrentInstance()
const emit = defineEmits(['submit'])

const show = ref(false)
const title = ref('')
const buttonLoading = ref(false)
const isView = ref(false)
const regionIds = ref([])

const data = reactive({
  form: {
    id: null,
    venueId: null,
    name: null,
    photos: null,
    province: null,
    city: null,
    area: null,
    address: null,
    introduction: null,
    status: '0',
    delStatus: '0'
  },
  rules: {
    name: [
      { required: true, message: '饭店名称不能为空', trigger: 'blur' }
    ],
    photos: [
      { required: true, message: '饭店图片不能为空', trigger: 'blur' }
    ],
    address: { validator: (rule, value, callback) => {
      if (!form.value.address || regionIds.value.length == 0) {
        callback(new Error('饭店地址不能为空'))
      } else {
        callback()
      }
    }, required: true, trigger: 'blur' },
    introduction: [
      { required: true, message: '饭店简介不能为空', trigger: 'blur' }
    ]
  }
})
const { form, rules } = toRefs(data)

const regionsList = computed(() => useUserStore().regionsList)

function submitForm() {
  proxy.$refs['restaurantRef'].validate(valid => {
    if (valid) {
      form.value.province = regionIds.value[0]
      form.value.city = regionIds.value[1]
      form.value.area = regionIds.value[2]

      buttonLoading.value = true
      if (form.value.id != null) {
        updateRestaurant(form.value).then(response => {
          proxy.$modal.msgSuccess('修改成功')
          show.value = false
          emit('submit')
        }).finally(() => {
          buttonLoading.value = false
        })
      } else {
        addRestaurant(form.value).then(response => {
          proxy.$modal.msgSuccess('新增成功')
          show.value = false
          emit('submit')
        }).finally(() => {
          buttonLoading.value = false
        })
      }
    }
  })
}

watch(show, (val) => {
  if (!val) {
    reset()
  }
})

function reset() {
  form.value = {
    id: null,
    venueId: null,
    name: null,
    photos: null,
    province: null,
    city: null,
    area: null,
    address: null,
    introduction: null,
    status: '0',
    delStatus: '0'
  }
  proxy.resetForm('restaurantRef')
}

function open(params) {
  reset()

  show.value = true
  title.value = params.title
  isView.value = params.isView || false

  if (params.restaurant) {
    form.value = params.restaurant

    regionIds.value = []
    if (params.restaurant.province) {
      regionIds.value.push(params.restaurant.province)
    }
    if (params.restaurant.city) {
      regionIds.value.push(params.restaurant.city)
    }
    if (params.restaurant.area) {
      regionIds.value.push(params.restaurant.area)
    }
  } else {
    form.value.venueId = useVenStore().venueInfo.id
  }
}
defineExpose({
  open
})
</script>

<style scoped>

</style>
